#include "stdafx.h"
#include "CppUnitTest.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace std;

namespace NativeTest_Question24
{
	typedef struct node{
		int val;
		node* Next;
	} node;

	node* Init(int* a, int n)
	{
		node* head = NULL;
		node* data = NULL;
		for (int i = 0; i < n; i++)
		{
			node* tn = new node();
			tn->val= a[i];
			tn->Next = NULL;
			if(i == 0) { head = tn; data = tn;}
			else { data->Next = tn, data = tn;}
		}
		return head;
	}

	node* Add(node* first, node* second)
	{
		node*  result = NULL;
		node* current = NULL;
		int index = 0;
		int more = 0;
		int val = 0;
		while (first || second)
		{
			int firstval = first == NULL? 0:first->val;
			int secondval = second == NULL? 0: second->val;
			
			val = (firstval + secondval + more) % 10;
			more = (firstval + secondval + more) /10;

			node* data = new node();
			data->val = val;
			data->Next = NULL;
			if(index++ == 0)
			{
				result = data; current = data;
			}
			else
			{
				current->Next = data; current =data;
			}
			if(first) first = first->Next;
			if(second) second = second->Next;
		}
		return result;
	}

	 void AssertList(const node* actual, const int* expected,int count)
	{
		for (int i = 0; i < count; i++)
		{      
			Assert::AreEqual(expected[i], actual->val);
			actual = actual->Next;
		}
	}
}

namespace NativeTest
{
	using namespace NativeTest_Question24;

	TEST_CLASS(Question24)
	{
	public:

		TEST_METHOD(Question24_TestMethod2)
		{
			node* result = Add(NULL, NULL);
			Assert::IsNull(result);
		}

		TEST_METHOD(Question24_TestMethod1)
		{
			int first[] = {1,2,9,9,3};
			int second[] = { 9,9,2};
			node* result = Add(Init(first, 5), Init(second, 3));

			int expected[] = {0,2,2,0,4};
			AssertList(result, expected, 5);
		}

		TEST_METHOD(Question24_TestMethod3)
		{
			int first[] = {11,2,9,9,3};
			int second[] = { 9,9,2};
			node* result = Add(Init(first, 5), Init(second, 3));

			int expected[] = {0,3,2,0,4};
			AssertList(result, expected, 5);
		}


	};
}